<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Echo by WebSocket</title>
</head>
<body>
<h1>Echo Test</h1>
<input id="sendTxt" type="text" />
<button id="sendBtn">发送</button>
<div id="recv"></div>
<script type="text/javascript">
    var websocket  = new WebSocket("ws://echo.websocket.org/");
    websocket.onopen = function () {
        console.log('websocket open');
        document.getElementById('recv').innerHTML='Connected';

    }
    websocket.onclose= function () {
        console.log('websocket close');
    }
    websocket.onmessage = function (p1) {
        console.log(p1.data);
        document.getElementById('recv').innerHTML= p1.data;
    }
    document.getElementById('sendBtn').onclick=function () {
        var txt = document.getElementById('sendTxt').value;
        websocket.send(txt);
    }

</script>
</body>
</html>